package com.beyondbit.hr.report.service.impl;

import com.beyondbit.hr.api.utils.NonRollbackException;
import com.beyondbit.hr.report.service.ReportService;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper;
import net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

public class DefaultReportService implements ReportService, InitializingBean {

    @Autowired
    DataSource dataSource;

    @Override
    public JasperReport jasperReport(String reportName) throws NonRollbackException {
        try {
            String rn = reportName.split("\\.")[0];
            InputStream inputStream = getClass().getResourceAsStream(String.format("/report/%s.jrxml",rn));
            return jasperReport(inputStream);
        }catch (Throwable e){
            throw new NonRollbackException(e);
        }
    }

    @Override
    public JasperReport jasperReport(InputStream inputStream) throws NonRollbackException {
        try {
            JasperDesign jd = JRXmlLoader.load(inputStream);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            return jr;
        } catch (JRException e) {
            throw new NonRollbackException( e);
        }

    }

    @Override
    public JasperPrint jasperPrint(JasperReport jr, Map<String, Object> params, Connection connection) throws NonRollbackException{
        try {
            return JasperFillManager.fillReport(jr, params, connection);
        } catch (JRException e) {
            throw new NonRollbackException( e);
        }

    }

    @Override
    public JasperPrint  jasperPrint(JasperReport jr, HttpServletRequest httpServletRequest) throws NonRollbackException{
        try(Connection c = dataSource.getConnection()){
            Map<String,Object> map = new HashMap<>();
            for (Map.Entry<String, String[]> entry : httpServletRequest.getParameterMap().entrySet()){
                if (entry.getValue()!=null){
                    String value = null;
                    if (entry.getValue().length > 0)
                        value = entry.getValue()[0];

                    map.put(entry.getKey(),value);
                }
            }

            return jasperPrint(jr,map,c);

        }catch (Throwable e){
            throw new NonRollbackException(e);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        //SimpleFontExtensionHelper.getInstance().loadFontExtensions();
        //SimpleFontExtensionsRegistryFactory
        JasperReportsContext context = DefaultJasperReportsContext.getInstance();

        context.setProperty("net.sf.jasperreports.default.pdf.font.name", "STSong-Light");
        context.setProperty("net.sf.jasperreports.default.pdf.encoding", "UniGB-UCS2-H");
        context.setProperty("net.sf.jasperreports.default.pdf.embedded", "true");


    }
}
